package com.elinshaw.wordcount;

import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * 消息源Spout类：随机选取一句英文语句作为源，发射出去
 *
 * @author Administrator
 */
public class WordCountSpout extends BaseRichSpout {

    private static final long serialVersionUID = -7931319401402491006L;
    private SpoutOutputCollector spoutoutputcollector;

    /**
     * 进行Spout的一些初始化工作
     */
    @Override
    public void open(Map conf, TopologyContext topologycontext, SpoutOutputCollector spoutoutputcollector) {
        this.spoutoutputcollector = spoutoutputcollector;
    }

    /**
     * 每两秒钟发射一次数据
     */
    @Override
    public void nextTuple() {
        this.spoutoutputcollector.emit(new Values("hello storm"));
    }

    /**
     * 定义字段名称，对应emit(new Values(sentence))中的字段
     */
    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputfieldsdeclarer) {
        outputfieldsdeclarer.declare(new Fields("line"));
    }

}  